function ModelCreator(Model) {
  class ModelExtend extends Model {
    constructor(...args) {
      super(...args)
    }

    get _rec_names_search() {
      return ['name', 'code']
    }

    async web2_name_search(kwargs = {}) {
      const fields = { display_name: {}, name: {}, code: {} }

      const order = 'code'
      const res = await super.web2_name_search({ ...kwargs, fields, order })

      const { records } = res
      const records2 = records.map(item => {
        return { ...item, display_name: `${item.code} ${item.name}` }
      })
      return { ...res, records: records2 }
    }

    async name_search(kwargs = {}) {
      // const { name, args, operator, limit } = kwargs
      // console.log(kwargs)

      const { name, args = [], limit } = kwargs

      const domain_get = () => {
        if (!name) {
          return args
        } else {
          const dm = ['|', ['name', 'ilike', name], ['code', 'ilike', name]]
          return [...dm, ...args]
        }
      }

      const domain = domain_get()
      const res2 = await this.search_read({
        domain,
        fields: ['name', 'code'],
        limit,
        order: 'code'
      })

      const res3 = res2.map(item => {
        return [item.id, `${item.code} ${item.name}`]
      })

      // console.log(res3)
      // const res = await super.name_search(kwargs)
      // console.log(res)
      return res3
    }
  }

  return ModelExtend
}

function StateModelCreator(Model) {
  class ModelExtend extends Model {
    constructor(...args) {
      super(...args)
    }
  }

  return ModelExtend
}

const metadata = {
  name: {
    testdomain() {
      return []
    }
  }
}

const state_metadata = {}

const AddonsModels = {
  'res.country': { ModelCreator, metadata },
  'res.country.state': {
    ModelCreator: StateModelCreator,
    metadata: state_metadata
  }
}

export default AddonsModels
